package com.example.springbootdemo.controller;

import com.example.springbootdemo.bean.CompareTimeReq;
import com.example.springbootdemo.bean.ResultData;
import com.example.springbootdemo.bean.kernelApi.ThingData;
import com.example.springbootdemo.service.KernelApiService;
import lombok.extern.log4j.Log4j2;
import lombok.var;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.security.auth.message.AuthException;
import javax.servlet.http.HttpServletRequest;
import java.security.SignatureException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Log4j2
@RestController
@RequestMapping("/work")
public class WorkController {

    @Autowired
    KernelApiService kernelApiService;


    @PostMapping("/compareTime")
    public ResultData<Boolean> compareTime(@RequestBody CompareTimeReq entity, HttpServletRequest request) throws AuthException, SignatureException {
        var token = request.getHeader("Authorization");
        if (!StringUtils.hasText(token)) {
            return new ResultData<>(false, false, "登录信息无效");
        }
        try {
            kernelApiService.connect();
            kernelApiService.tokenAuth(token);

            var list = new ArrayList<ThingData>();
            getData(1, list);
            return judgeOrder(list, entity.getStartTime(), entity.getEndTime());
        } catch (Exception ex) {
            log.error("获取数据失败", ex);
            return new ResultData<>(false, false, ex.getMessage());
        }
    }

    private ResultData<Boolean> judgeOrder(List<ThingData> list, String startTime, String endTime) throws ParseException {
        if (StringUtils.hasText(startTime) && StringUtils.hasText(endTime)) {
            var formart = "yyyy-MM-dd HH:mm:ss";
            var startL = DateUtils.parseDate(startTime, formart).getTime();
            var endL = DateUtils.parseDate(endTime, formart).getTime();

            if (!list.isEmpty()) {
                for (ThingData thing :
                        list) {
                    var _startL = DateUtils.parseDate(thing.getT472763800383262720(), formart).getTime();
                    var _endL = DateUtils.parseDate(thing.getT472764320720228352(), formart).getTime();

                    if (startL >= _startL && startL <= _endL) {
                        return new ResultData<>(false, false, "该时间段已被预约，请重新选择");
                    }

                    if (endL >= _startL && endL <= _endL) {
                        return new ResultData<>(false, false, "该时间段已被预约，请重新选择");
                    }

                    if (_startL <= startL && _endL >= endL) {
                        return new ResultData<>(false, false, "该时间段已被预约，请重新选择");
                    }
                }
            }
        } else {
            return new ResultData<>(false, false, "时间必填");
        }
        return new ResultData<>(true, true, "比对通过");
    }

    private void getData(int index, List<ThingData> list) {
        var data = kernelApiService.loadThing("445723536485650432", index, 20);
        if (data != null && data.getData().getData() != null) {
            for (ThingData thing :
                    data.getData().getData()) {
                if (thing.getT472763800383262720() == null) {
                    return;
                } else {
                    list.add(thing);
                }
            }
            getData(index + 1, list);
        }
    }
}
